package cn.zhangjie.controller;

import cn.zhangjie.domain.JsonResult;
import cn.zhangjie.domain.Store;
import cn.zhangjie.mapper.StoreMapper;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/store")
@RefreshScope  //刷新配置
public class StoreController {

    @Autowired
    private StoreMapper storeMapper;

    @GetMapping("/findone/{id}")
    @SentinelResource(value="productfindone",blockHandler="exceptionHandler")
    public Store findone(@PathVariable("id") Long id){
        return storeMapper.findone(id);
    }
    // 限流与阻塞处理 : 参数要和 被降级的方法参数一样
    public Store exceptionHandler(@PathVariable Long id, BlockException ex) {
        ex.printStackTrace();
        System.out.println("限流了...");
        return new Store(id,1l,1);
    }

    @PutMapping("/add")
    @SentinelResource(value="productadd",blockHandler="exceptionHandler")
    public JsonResult add(@RequestBody Store store){
        storeMapper.add(store);
        return JsonResult.me();
    }




}